from app.models import BaseModel
from tools import db


class Classes(db.Model, BaseModel):
    __tablename__ = "api_classes"
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    name = db.Column(db.String(120), unique=True, index=True)
    grade = db.Column(db.String(120))
    # major = db.Column(db.String(120))

    student_set = db.relationship("User", backref="classes")

    def __repr__(self):
        return "Class:%s" % self.name

    def to_dict(self):
        return {
            "id": self.id,
            "name": self.name,
            "grade": self.grade,
            "users": [_.to_dict() for _ in self.student_set],
            "users_count": len(self.student_set) if len(self.student_set) else None,
        }
